//
//  ABUDCustomInterstitialAdapter.m
//  ABUDemo
//
//  Created by bytedance on 2021/11/3.
//  Copyright © 2021 bytedance. All rights reserved.
//

#import "JiGuangAdsInterstitialAdapter.h"
#import <JgAdsSDK/JgAdsInterstitialAd.h>
@interface JiGuangAdsInterstitialAdapter ()<JgAdsInterstitialAdDelegate>

@property (nonatomic, strong) JgAdsInterstitialAd *interstitialAd;

@property (nonatomic, weak) UIViewController *viewController;

@property (nonatomic, assign) BOOL isBidType;

@end

@implementation JiGuangAdsInterstitialAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadInterstitialAdWithSlotID:(NSString *)slotID andSize:(CGSize)size parameter:(NSDictionary *)parameter {
    _interstitialAd = [JgAdsInterstitialAd new];;
    _interstitialAd.delegate = self;
    _interstitialAd.posId = slotID;
    _interstitialAd.controller = self.bridge.viewControllerForPresentingModalView;
    // 是否开启倒计时关闭
//     _interstitialAd.isAutoClose = YES;
    // 设置倒计时关闭时长 [3~100) 区间内有效
//     _interstitialAd.autoCloseTime = 10;
    [_interstitialAd loadAdData];
    if([parameter[BUMAdLoadingParamBiddingType] integerValue] == 1){
        self.isBidType = YES;
    }else{
        self.isBidType = NO;
    }
       if (self.isBidType) {
           NSLog(@"极光ads插屏广告==========竞价广告");
       }

}

- (BOOL)showAdFromRootViewController:(UIViewController *)viewController parameter:(NSDictionary *)parameter {
    self.viewController = viewController;
    if (self.isBidType) {
        [self.interstitialAd sendWinNotificationWithPrice:self.interstitialAd.bidPrice];
    }
    [self.interstitialAd showFromRootViewController:self.viewController];
    return YES;
}

-(void)didReceiveBidResult:(BUMMediaBidResult *)result{
    
    if (!self.interstitialAd) return;
    if (result.win) {
        NSLog(@"插屏==========竞价成功!竞赢价格：%ld",(long)result.winnerPrice);
    } else {
        NSLog(@"插屏==========竞价失败:%@",result.lossDescription);
        JgAdsBiddingLossReason reason ;
        if ([result.lossDescription isEqualToString:@"timeout"]) {
            reason = JgAdsBiddingLossReasonOther;
        }else if ([result.lossDescription isEqualToString:@"low price"]){
            reason = JgAdsBiddingLossReasonLowPrice;
        }else {
            reason = JgAdsBiddingLossReasonOther;
        }
        [self.interstitialAd sendWinFailNotificationReason:reason winnerPirce:result.winnerPrice];
       
    }
}

#pragma mark - JgAdsInterstitialAdDelegate

/**
 *  插屏广告数据请求成功
 */
- (void)jgadsInterstitialSuccessToLoadAd:(JgAdsInterstitialAd *)unifiedInterstitial {
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    NSLog(@"极光ads插屏竞价价格：%ld",(long)unifiedInterstitial.bidPrice);
        if (self.isBidType && unifiedInterstitial.bidPrice > 0) {
            [parames setValue:[NSString stringWithFormat:@"%ld",unifiedInterstitial.bidPrice] forKey:BUMMediaAdLoadingExtECPM];
        }
    [self.bridge interstitialAd:self didLoadWithExt:parames];
}

/**
 *  插屏广告数据请求失败
 */
- (void)jgadsInterstitialFailToLoadAd:(JgAdsInterstitialAd *)unifiedInterstitial error:(NSError *)error {
    NSLog(@"极光ads插屏加载失败：%@",error);
    [self.bridge interstitialAd:self didLoadFailWithError:error ext:@{}];
}

/**
 *  插屏广告渲染成功
 *  建议在此回调后展示广告
 */
- (void)jgadsInterstitialRenderSuccess:(JgAdsInterstitialAd *)unifiedInterstitial {
    [self.bridge interstitialAdRenderSuccess:self];
}

/**
 *  插屏广告视图展示成功回调
 *  插屏广告展示成功回调该函数
 */
- (void)jgadsInterstitialDidPresentScreen:(JgAdsInterstitialAd *)unifiedInterstitial {

}

/**
 *  插屏广告视图展示失败回调
 *  插屏广告展示失败回调该函数
 */
- (void)jgadsInterstitialFailToPresent:(JgAdsInterstitialAd *)unifiedInterstitial error:(NSError *)error {
    NSLog(@"极光ads插屏展示失败：%@",error);
    [self.bridge interstitialAdDidShowFailed:self error:error];
}

/**
 *  插屏广告曝光回调
 */
- (void)jgadsInterstitialWillExposure:(JgAdsInterstitialAd *)unifiedInterstitial {
    [self.bridge interstitialAdDidVisible:self];
}

/**
 *  插屏广告点击回调
 */
- (void)jgadsInterstitialClicked:(JgAdsInterstitialAd *)unifiedInterstitial {
    [self.bridge interstitialAdDidClick:self];
    [self.bridge interstitialAdWillPresentFullScreenModal:self];
}

/**
 *  插屏广告页关闭
 */
- (void)jgadsInterstitialAdDidDismissClose:(JgAdsInterstitialAd *)unifiedInterstitial {
    [self.bridge interstitialAdDidClose:self];
}
/**
 *  插屏广告落地页关闭
 */
- (void)jgadsInterstitialAdDidCloseLandingPage:(JgAdsInterstitialAd *)unifiedInterstitial{
    [self.bridge interstitialAdWillDismissFullScreenModal:self];
}

/**
 *以下为视频插屏相关回调
 */

/**
 插屏视频广告开始播放
 */
- (void)jgadsInterstitialAdVideoPlay:(JgAdsInterstitialAd *)unifiedInterstitial {
    
}

/**
 插屏视频广告视频播放失败
 */
- (void)jgadsInterstitialAdVideoPlayFail:(JgAdsInterstitialAd *)unifiedInterstitial error:(NSError *)error {
    
}

/**
 插屏视频广告视频暂停
 */
- (void)jgadsInterstitialAdVideoPause:(JgAdsInterstitialAd *)unifiedInterstitial {
    
}

/**
 插屏视频广告视频播放完成
 */
- (void)jgadsInterstitialAdVideoFinish:(JgAdsInterstitialAd *)unifiedInterstitial {
    
}


@end
